package com.example.helloworldtext2.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.helloworldtext2.R;
import com.example.helloworldtext2.model.Article;
import com.example.helloworldtext2.holder.RecyclerItemNormalHolder;

import java.util.ArrayList;
import java.util.List;

public class VideoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final static String TAG = "ViewPagerAdapter";

    private List<Article> itemDataList = null;
    private Context context = null;
    private List<RecyclerItemNormalHolder> holderList = new ArrayList<>();

    public VideoAdapter(Context context, List<Article> itemDataList) {
        Log.d("sfsdfds","视频大小" + itemDataList.size());
        this.itemDataList = itemDataList;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,
                                                      int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.item_video, parent, false);
        final RecyclerView.ViewHolder holder = new RecyclerItemNormalHolder(context, v);
        return holder;

    }



    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        RecyclerItemNormalHolder recyclerItemViewHolder = (RecyclerItemNormalHolder) holder;
        recyclerItemViewHolder.setRecyclerBaseAdapter(this);
        recyclerItemViewHolder.onBind(position, itemDataList.get(position));
        holder.itemView.setTag(position);
        // 添加到holder列表（如果尚未添加）
        if (!holderList.contains(recyclerItemViewHolder)) {
            holderList.add(recyclerItemViewHolder);
        }
    }
    
    @Override
    public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewRecycled(holder);
    }
    
    @Override
    public boolean onFailedToRecycleView(@NonNull RecyclerView.ViewHolder holder) {
        return true; // 允许回收，避免潜在问题
    }

    @Override
    public int getItemCount() {
        return itemDataList.size();
    }


    @Override
    public int getItemViewType(int position) {
        return 1;
    }
    @Override
    public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
        Log.d("onViewAttachedToWindow", "进入屏幕");
        ImageView coverImage = holder.itemView.findViewById(R.id.coverImage);
        coverImage.setVisibility(View.VISIBLE);
    }

    /**
     * Called when a view created by this adapter has been detached from its window.
     *
     * <p>Becoming detached from the window is not necessarily a permanent condition;
     * the consumer of an Adapter's views may choose to cache views offscreen while they
     * are not visible, attaching and detaching them as appropriate.</p>
     *
     * @param holder Holder of the view being detached
     */
    @Override
    public void onViewDetachedFromWindow(@NonNull RecyclerView.ViewHolder holder) {
        Log.d("onViewAttachedToWindow", "离开屏幕");
    }
}